package com.ncd.disease.controller;

import com.ncd.common.annotation.Log;
import com.ncd.common.core.controller.BaseController;
import com.ncd.common.core.domain.AjaxResult;
import com.ncd.common.core.page.TableDataInfo;
import com.ncd.common.core.text.Convert;
import com.ncd.common.enums.BusinessType;
import com.ncd.common.utils.ServletUtils;
import com.ncd.common.utils.poi.ExcelUtil;
import com.ncd.disease.domain.RetPatient;
import com.ncd.disease.domain.RetPatientInfo;
import com.ncd.disease.param.RetPatientParam;
import com.ncd.disease.param.RetPatientQueryParam;
import com.ncd.disease.service.IRetPatientInfoService;
import com.ncd.disease.service.IRetPatientService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 患者基本信息Controller
 * 
 * @author zhouxin
 * @date 2024-03-07
 */
@Controller
@RequestMapping("/disease/patient")
public class RetPatientController extends BaseController
{
    private String prefix = "disease/patient";

    @Resource
    private IRetPatientService retPatientService;

    @Resource
    IRetPatientInfoService retPatientInfoService;

    @RequiresPermissions("disease:patient:view")
    @GetMapping()
    public String patient(ModelMap mmap)
    {
        mmap.put("dia", ServletUtils.getParameter("dia"));
        mmap.put("admType", ServletUtils.getParameter("admType"));
        return prefix + "/patient";
    }

    /**
     * 查询患者基本信息列表
     */
    @RequiresPermissions("disease:patient:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RetPatientQueryParam retPatient)
    {
        startPage();
        List<RetPatient> list = retPatientService.selectRetPatientList(retPatient);
        return getDataTable(list);
    }

    /**
     * 导出患者基本信息列表
     */
    @RequiresPermissions("disease:patient:export")
    @Log(title = "患者基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RetPatientQueryParam retPatient)
    {
        List<RetPatient> list = retPatientService.selectRetPatientList(retPatient);
        ExcelUtil<RetPatient> util = new ExcelUtil<RetPatient>(RetPatient.class);
        return util.exportExcel(list, "患者基本信息数据");
    }

    /**
     * 新增患者基本信息
     */
    @GetMapping("/add/{diaId}/{admType}")
    public String add(@PathVariable("diaId") Long diaId,@PathVariable("admType") String admType, ModelMap mmap)
    {
        mmap.put("diaId", diaId);
        mmap.put("admType", admType);
        return prefix + "/add";
    }

    /**
     * 新增保存患者基本信息
     */
    @RequiresPermissions("disease:patient:add")
    @Log(title = "患者基本信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RetPatient retPatient)
    {
        return toAjax(retPatientService.insertRetPatient(retPatient));
    }

    /**
     * 修改患者基本信息
     */
    @RequiresPermissions("disease:patient:edit")
    @GetMapping("/edit/{id}/{diaId}")
    public String edit(@PathVariable("id") Long id,@PathVariable("diaId") Long diaId, ModelMap mmap)
    {
        RetPatient retPatient = retPatientService.selectRetPatientById(id);
        mmap.put("retPatient", retPatient);
        RetPatientInfo retPatientInfo=retPatientInfoService.selectRetPatientInfoByPatId(id,diaId);
        mmap.put("retPatientInfo", retPatientInfo);
        mmap.put("diaId", diaId);
        return prefix + "/edit";
    }

    /**
     * 修改保存患者基本信息
     */
    @RequiresPermissions("disease:patient:edit")
    @Log(title = "患者基本信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody RetPatientParam param)
    {
        return toAjax(retPatientService.updateRetPatient(param));
    }

    /**
     * 获取患者信息
     */
    @GetMapping("/info/{id}")
    @ResponseBody
    public AjaxResult info(@PathVariable("id") Long id)
    {
        return AjaxResult.success(retPatientService.selectRetPatientById(id));
    }

    /**
     * 删除患者基本信息
     */
    @RequiresPermissions("disease:patient:remove")
    @Log(title = "患者基本信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(retPatientService.removeByIds(Convert.toLongList(ids)));
    }
}
